public class MaxProfit {
    public int maxProfit(int[] prices) {
        int length = prices.length;
        int profit=0;
        int max=prices[length-1];
        for (int i = prices.length-1; i >=0 ; i--) {
            max=Math.max(max,prices[i]);
            profit=Math.max(profit,max-prices[i]);
        }
        return profit;
    }
}
